<?php

	include("./dbconfig.php");
	include("../../public/functions.php");
	
	$link = @mysqli_connect(HOST,USER,PASS) or die("数据库连接失败");
	mysqli_select_db($link,DBNAME);
	mysqli_set_charset($link,"utf8");
	
	$upfile = $_FILES['picname'];
	$path = "../../public/uploads";
	$typelist = array("image/png","image/jpeg","image/jpg","image/gif","image/pjpeg"); 
	
	switch($_GET['action']){
		case 'insert':
		// echo $_POST['action'];die;
            // $id = $_POST['id'];
			$typeid = $_POST['typeid'];
            $goods = $_POST['goods'];
            $company = $_POST['company'];
            $descr = $_POST['descr'];
            $price = $_POST['price'];
            $picname = $upfile['name'];//上传图片的老名字
            $state = $_POST['state'];
            $store = $_POST['store'];
            $num = $_POST['num'];
            $clicknum = $_POST['clicknum'];
            $addtime = time();
			$uppic = fileupload($upfile,$path,$typelist);
            $picname= $uppic['info']; //存到数据里面的新名字
			// echo $upfile['type'];die;
			
			$sql = "insert into goods(typeid,goods,company,descr,price,picname,state,store,num,clicknum,addtime) values('$typeid','$goods','$company','$descr','$price','$picname','$state','$store','$num','$clicknum','$addtime')";
			// echo $sql;die;
			$result = mysqli_query($link,$sql);
			
			//压缩图片
			imageZoom($picname,"../../public/uploads/",$width=40,$height=40,$pre="s_");
			
            if(mysqli_insert_id($link)>0){
                header("Location:index.php");
				die;
            }else{
                echo "添加失败";
            }
           echo mysqli_error($link);
		break;

        case 'del':
            $id = $_GET['id'];
            $sql = "delete from goods where id={$id}";
			echo $sql;
            
            $result = mysqli_query($link,$sql);

            
            if(mysqli_affected_rows($link)>0){
                header("Location:index.php");
            }else{
                echo "删除失败";
            }
            
        break;
        
        case 'update':
            $id = $_POST['id'];
			$typeid = $_POST['typeid'];
            $goods = $_POST['goods'];
            $company = $_POST['company'];
            $descr = $_POST['descr'];
            $price = $_POST['price'];
            $picname = $_FILES['picname'];
			$picname = fileupload($picname,'../../public/uploads')['info'];
            $state = $_POST['state'];
            $store = $_POST['store'];
            $num = $_POST['num'];
            $clicknum = $_POST['clicknum'];
            $addtime = time();
           
            $sql = "update goods set typeid='$typeid',goods='$goods',company='$company',descr='$descr',price='$price',picname='$picname',state='$state',store='$store',num='$num',clicknum='$clicknum',addtime='$addtime' where id={$_POST['id']}";
            
            $result = mysqli_query($link,$sql);
			
			imageZoom($picname,"../../public/uploads/",$width=40,$height=40,$pre="s_");
               
            header("Location:index.php");
            
        break;
	}